<?php
/*
 * (C) Copyright 2013 Torsten Gorath (http://www.blauesdach.de/).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Contributors:
 *     Torsten Gorath
 */
require_once $basedir . "/inc/kinder.php";
?>
<div id='blkEdit' style='margin-bottom: 10px'>
    <div class='inline-block label'>
        Kind:
    </div>
    <div class='inline-block'>
        <input id='txtChild' value=""/>
    </div>
    <div class='inline-block label'>
        <button id='btnNewChild'>Neues Kind</button>
    </div>
</div>
<div>
    <input type='hidden' id='kind_id' name='kind_id' value='' />
    <div class='inline-block' style='width: 49%'>
        <div>
            <div class='inline-block'>
                <div class='inline-block'>
                    <div class='inline-block label'>Vorname:</div>
                    <div class='inline-block'>
                        <input type='text' id='kind_vorname' value='' />
                    </div>
                </div>
                <div>
                    <div class='inline-block label'>Nachname:</div>
                    <div class='inline-block'>
                        <input type='text' id='kind_nachname' value='' />
                    </div>
                </div>
            </div>
            <div class='inline-block'>
                <input type='checkbox' id='kind_allow_name' /> Name ver&ouml;ffentlichen
            </div>
        </div>
        <div>
            <div class='inline-block label'>Geburtsdarum:</div>
            <div class='inline-block'>
                <input type='text' name='kind_birthday_am' id='kind_birthday_am' value='' />
            </div>
        </div>
        <div>
            <div class='inline-block'>
                <div class='inline-block'>
                    <div class='inline-block label'><a href="#address">Adresse</a>:</div>
                    <div class='inline-block'>
                        <input type='hidden' id='kind_address_id' value='' />
                        <select id='kind_address_from'>
                            <option value='' selected="selected">Keine</option>
                            <option value='mother'>Mutter</option>
                            <option value='father'>Vater</option>
                        </select>
                    </div>
                </div>
                <div>
                    <div class='inline-block label'>Strasse:</div>
                    <div class='inline-block'>
                        <input type='text' id='kind_strasse' name='kind_strasse' value='' />
                    </div>
                </div>
                <div>
                    <div class='inline-block label'>Hausnummer:</div>
                    <div class='inline-block'>
                        <input type='text' id='kind_street_number' name='kind_street_number' value='' />
                    </div>
                </div>
                <div>
                    <div class='inline-block label'>Hausnummer Zusatz:</div>
                    <div class='inline-block'>
                        <input type='text' id='kind_street_number_enhencement' name='kind_street_number_enhencement' value='' />
                    </div>
                </div>
                <div>
                    <div class='inline-block label'>Postleitzahl:</div>
                    <div class='inline-block'>
                        <input type='text' id='kind_zip' name='kind_zip' value='' />
                    </div>
                </div>
                <div>
                    <div class='inline-block label'>Ort:</div>
                    <div class='inline-block'>
                        <input type='text' id='kind_city' name='kind_city' value='' />
                    </div>
                </div>
            </div>
            <div class='inline-block'>
                <input type='checkbox' id='kind_allow_address' name='kind_allow_address' /> Adresse ver&ouml;ffentlichen
            </div>
        </div>
        <div>
            <div class='inline-block'>
                <div class='inline-block'>
                    <div class='inline-block label'><a href="#mother">Mutter</a>:</div>
                    <div class='inline-block'>
                        <input type='hidden' id='kind_mother_id' name='kind_mother_id' value='' />
                        <input type='text' name='kind_mother_auto_address' value='' />
                    </div>
                </div>
            </div>
            <div class='inline-block'>
                <input type='checkbox' id='kind_mother_sorgerecht' /> Sorgeberechtigt
            </div>
        </div>
        <div>
            <div class='inline-block'>
                <div class='inline-block'>
                    <div class='inline-block label'><a href="#father">Vater</a>:</div>
                    <div class='inline-block'>
                        <input type='hidden' id='kind_father_id' name='kind_father_id' value='' />
                        <input type='text' name='kind_father_auto_address' value='' />
                    </div>
                </div>
            </div>
            <div class='inline-block'>
                <input type='checkbox' id='kind_father_sorgerecht' /> Sorgeberechtigt
            </div>
        </div>
        <div>
            <div class='inline-block label'>Zur&uuml;ckgestellt:</div>
            <div class='inline-block'>
                <input type='text' name='kind_set_back' value='' />
            </div>
        </div>
        <div>
            <input type="checkbox" id="kind_allow_image_print" name="kind_allow_image_print" />
            Erlaubnis Bilder als Druck zu verwenden
        </div>
        <div>
            <input type="checkbox" id="kind_allow_image_online" name="kind_allow_image_online" />
            Erlaubnis Bilder im Internet zu verwenden
        </div>
    </div>
    <div class='inline-block' style='width: 49%'>
        <div id='contracts'>
            <div>
                <button id='contracts_new'>Neu</button>
                <button id='contracts_edit'>&Auml;ndern</button>
                <button id='contracts_delete'>L&ouml;schen</button>
            </div>
            <div>
                <select id='contracts_list' style='width:100%;' size=5 multiple="false"></select>
            </div>
        </div>
        <div id='blk_contract' style='display: none;'>
            <input type='hidden' id='contract_id' value='' />
            <div>
                <div class='inline-block label'>Bankverbindung:</div>
                <div class='inline-block'>
                    <input name="bank_id" id='bank_id' type="hidden" value="" />
                    <input type="text" name="bank_name" id='bank_name' maxlength="100" value="" />
                </div>
            </div>
            <div>
                <div class='inline-block'>
                    <div class='inline-block label'>Vertrag ab:</div>
                    <div class='inline-block'>
                        <input type='date' name='vertrag_ab' id='vertrag_ab' value='' />
                    </div>
                </div>
                <div class='inline-block'>
                    <div class='inline-block label'>Vertrag bis:</div>
                    <div class='inline-block'>
                        <input type='date' name='ausgeschieden_am' id='ausgeschieden_am' value='' />
                    </div>
                </div>
            </div>
            <div>
                <div class='inline-block label'>Betreuung von:</div>
                <div class='inline-block'>
                    <input type='time' name='betreuung_von' id='betreuung_von' value='' />
                </div>
            </div>
            <div>
                <div class='inline-block label'>Betreuung bis:</div>
                <div class='inline-block'>
                    <input type='time' name='betreuung_bis' id='betreuung_bis' value='' />
                </div>
            </div>
            <div>
                <div class='inline-block'>
                    <div class='inline-block label'>Elterndarlehen:</div>
                    <div class='inline-block'>
                        <input type='number' name='darlehen_summe' id='darlehen_summe' value='' />
                    </div>
                </div>
                <div class='inline-block'>
                    <div>
                        <div class='inline-block label'>Eingezogen:</div>
                        <div class='inline-block'>
                            <input type='number' name='darlehen_eingezogen_am' id='darlehen_eingezogen_am' value='' />
                        </div>
                    </div>
                    <div>
                        <div class='inline-block label'>Zur&uuml;ck:</div>
                        <div class='inline-block'>
                            <input type='number' name='darlehen_zurueck_am' id='darlehen_zurueck_am' value='' />
                        </div>
                    </div>
                </div>
            </div>
            <div>
                <div class='inline-block'>
                    <div class='inline-block label'>Aufnahmegeb&uuml;hr:</div>
                    <div class='inline-block'>
                        <input type='number' name='aufnahmegebuehr_summe' id='aufnahmegebuehr_summe' value='' />
                    </div>
                </div>
                <div class='inline-block'>
                    <div class='inline-block label'>Eingezogen:</div>
                    <div class='inline-block'>
                        <input type='number' name='aufnahmegebuehr_eingezogen_am' id='aufnahmegebuehr_eingezogen_am' value='' />
                    </div>
                </div>
            </div>
            <div>
                <div class='inline-block'>
                    <div class='inline-block label'>Baugeld:</div>
                    <div class='inline-block'>
                        <input type='number' name='baugeld_summe' id='baugeld_summe' value='' />
                    </div>
                </div>
                <div class='inline-block'>
                    <div class='inline-block label'>Eingezogen:</div>
                    <div class='inline-block'>
                        <input type='number' name='baugeld_eingezogen_am' id='baugeld_eingezogen_am' value='' />
                    </div>
                </div>
            </div>
            <div>
                <div class='inline-block'>
                    <div>
                        <div class='inline-block label'>Monatsbeitrag:</div>
                        <div class='inline-block'>
                            <input type='number' name='monatsbeitrag_summe' id='monatsbeitrag_summe' value='' />
                        </div>
                    </div>
                    <div>
                        <div class='inline-block label'>Spende:</div>
                        <div class='inline-block'>
                            <input type='number' name='spende_summe' id='spende_summe' value='' />
                        </div>
                    </div>
                    <div>
                        <div class='inline-block label'>Hygienebeitrag:</div>
                        <div class='inline-block'>
                            <input type='number' name='hygienebeitrag_summe' id='hygienebeitrag_summe' value='' />
                        </div>
                    </div>
                </div>
                <div class='inline-block'>
                    <div class='inline-block label'>Zuschuss Stadt:</div>
                    <div class='inline-block'>
                        <input type='number' name='zuschuss_stadt_summe' id='zuschuss_stadt_summe' value='' />
                    </div>
                </div>
            </div>
        </div>
        <div id='blk_address' style='display: none;'>
            <input type='hidden' id='address_id' name='address_id' value='' />
            <div>
                <div class='inline-block'>
                    <div class='inline-block'>
                        <div class='inline-block label'>Anrede:</div>
                        <div class='inline-block'>
                            <select id='address_anrede'>
                                <option value="Familie">Familie</option>
                                <option value="Frau">Frau</option>
                                <option value="Herr">Herr</option>
                            </select>
                        </div>
                    </div>
                </div>
                <div class='inline-block'>
                    <input type='checkbox' id='address_german' /> deutsch Staatsb&uuml;rgerschaft
                </div>
            </div>
            <div>
                <div class='inline-block'>
                    <div class='inline-block'>
                        <div class='inline-block label'>Vorname:</div>
                        <div class='inline-block'>
                            <input type='text' id='address_vorname' value='' />
                        </div>
                    </div>
                    <div>
                        <div class='inline-block label'>Nachname:</div>
                        <div class='inline-block'>
                            <input type='text' id='address_nachname' value='' />
                        </div>
                    </div>
                </div>
                <div class='inline-block'>
                    <input type='checkbox' id='address_allow_name' /> Name ver&ouml;ffentlichen
                </div>
            </div>
            <div>
                <div class='inline-block'>
                    <div class='inline-block'>
                        <div class='inline-block label'>Strasse:</div>
                        <div class='inline-block'>
                            <input type='text' id='address_street' value='' />
                        </div>
                    </div>
                    <div>
                        <div class='inline-block label'>Hausnummer:</div>
                        <div class='inline-block'>
                            <input type='text' id='address_street_number' value='' />
                        </div>
                    </div>
                    <div>
                        <div class='inline-block label'>Hausnummer Zusatz:</div>
                        <div class='inline-block'>
                            <input type='text' id='address_street_number_enhencement' value='' />
                        </div>
                    </div>
                    <div>
                        <div class='inline-block label'>Postleitzahl:</div>
                        <div class='inline-block'>
                            <input type='text' id='address_zip' value='' />
                        </div>
                    </div>
                    <div>
                        <div class='inline-block label'>Ort:</div>
                        <div class='inline-block'>
                            <input type='text' id='address_city' value='' />
                        </div>
                    </div>
                </div>
                <div class='inline-block'>
                    <input type='checkbox' id='address_allow_address' /> Addresse ver&ouml;ffentlichen
                </div>
            </div>
            <div>
                <div class='inline-block'>
                    <div class='inline-block'>
                        <div class='inline-block label'>Telefon:</div>
                        <div class='inline-block'>
                            <input type='text' id='address_phone' value='' />
                        </div>
                    </div>
                </div>
                <div class='inline-block'>
                    <input type='checkbox' id='address_allow_phone' /> Telefonnummer ver&ouml;ffentlichen
                </div>
            </div>
            <div>
                <div class='inline-block label'>Beruflich (Tel.):</div>
                <div class='inline-block'>
                    <input type='text' id='address_office' value='' />
                </div>
            </div>
            <div>
                <div class='inline-block'>
                    <div class='inline-block'>
                        <div class='inline-block label'>Mobile:</div>
                        <div class='inline-block'>
                            <input type='text' id='address_mobile' value='' />
                        </div>
                    </div>
                </div>
                <div class='inline-block'>
                    <input type='checkbox' id='address_allow_mobile' /> Mobilenummer ver&ouml;ffentlichen
                </div>
            </div>
            <div>
                <div class='inline-block'>
                    <div class='inline-block'>
                        <div class='inline-block label'>EMail:</div>
                        <div class='inline-block'>
                            <input type='text' id='address_email' value='' />
                        </div>
                    </div>
                </div>
                <div class='inline-block'>
                    <input type='checkbox' id='address_allow_email' /> E-Mailadresse ver&ouml;ffentlichen<br />
                    <input type='checkbox' id='address_rare_email' /> E-Mailadresse wird selten abgerufen<br />
                    <input type='checkbox' id='address_send_email' /> Erlaubnis E-Mails zu senden
                </div>
            </div>
            <div>
                <div class='inline-block label'>Beruf:</div>
                <div class='inline-block'>
                    <input type='text' id='address_job' value='' />
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    var hasChanges = false;
    var father = null;
    var mother = null;
    $(function() {
        $('#txtChild').autocomplete({
            source : function(request, response) {
                var data = {
                    'value' : request.term,
                    'page' : 'stammdaten_kinder',
                    'action' : 'search'
                };
                $.ajax({
                    'url' : "json.php",
                    'data' : data,
                    'type' : 'post',
                    'dataType' : 'json',
                    'success' : function(data) {
                        response(data.items);
                    }
                })
            },
            minLength : 2,
            select : function(event, ui) {
                var item = ui.item;
                if (hasChanges && confirm("Es gibt Änderungen, sollen diese gespeichert werden?")) {
                    // TODO
                    alert("Save");
                }
                var data = {
                    'id' : item.id,
                    'page': 'stammdaten_kinder',
                    'action' : 'get'
                };
                $.ajax({
                    'url' : 'json.php',
                    'data' : data,
                    'type' : 'post',
                    'dataType' : 'json',
                    'success' : function(data) {
                        getAddress(data.adresse_id, showChildAddress);
                        getAddress(data.mutter_id, storeMother);
                        getAddress(data.vater_id, storeFather);
                        showChildData(data);
                    }
                })
            },
            response : function(event, ui) {
                for (var i = 0; i < ui.content.length; i++) {
                    ui.content[i].label = ui.content[i].vorname + " " + ui.content[i].nachname;
                    ui.content[i].value = ui.content[i].label;
                }
            }
        });
        $( "#bank_name" ).autocomplete({
            source: function(request, response) {
                var data = {
                    'value': request.term,
                    'page': 'stammdaten_bank',
                    'action': 'search'
                };
                $.ajax({
                    'url': "json.php",
                    'data': data,
                    'type':'post',
                    'dataType': 'json',
                    'html': 'html',
                    'success': function(data) {
                        response(data.items);
                    }
                })
            },
            minLength : 2,
            select: function(event, ui) {
                $('input[name=bank_id]').attr('value', ui.item.id);
            },
            response : function(event, ui) {
                for (var i = 0; i < ui.content.length; i++) {
                    ui.content[i].label = ui.content[i].name + "<br />"
                        + "BLZ: " + ui.content[i].blz + "<br />"
                        + "Kontonr: " + ui.content[i].kontonummer;
                    ui.content[i].value = ui.content[i].name;
                }
            }
        });
        $('#btnNewChild').click(function() {
            resetForm();
            return false;
        });
        $('a[href$="mother"]').click(function() {
            $('div[id^="blk_"]').css('display', 'none');
            showAddress(mother);
            $('#blk_address').css('display', 'block');
        })
        $('a[href$="father"]').click(function() {
            $('div[id^="blk_"]').css('display', 'none');
            showAddress(father);
            $('#blk_address').css('display', 'block');
        });
        $('#contracts_new').click(function() {
            resetContract();
            $('#blk_contract').css('display', 'block');
        });
        $('#contracts_edit').click(function() {
            // TODO
            $('#blk_contract').css('display', 'block');
        });
        $('#contracts_delete').click(function() {
            // TODO
            $('#blk_contract').css('display', 'none');
        })
        
        function getAddress(id, callback) {
            var data = {
                'id' : id,
                'page': 'stammdaten_adress',
                'action' : 'get'
            };
            $.ajax({
                'url' : 'json.php',
                'data' : data,
                'type' : 'post',
                'dataType' : 'json',
                'success' : function(data) {
                    callback(data)
                }
            })
        }
        function getContracts(child_id, callback) {
            
        }
        function storeFather(data) {
            father = data;
            $('#kind_father_id').val(father.id);
            $('#kind_father_auto_address').val(father.vorname + " " + father.nachname);
        }
        function storeMother(data) {
            mother = data;
            $('#kind_mother_id').val(mother.id);
            $('#kind_mother_auto_address').val(mother.vorname + " " + mother.nachname);
        }
        function showAddress(data) {
            $('#address_id').val(data.id);
            $('#address_anrede option').removeAttr('selected');
            $('#address_anrede option[value="'+data.anrede+'"]').get(0).selected = true;
            $('#address_german').prop('checked', data.deutsch == "1");
            $('#address_vorname').val(data.vorname);
            $('#address_nachname').val(data.nachname);
            $('#address_allow_name').prop('checked', data.name_show == "1");
            $('#address_street').val(data.strasse);
            $('#address_street_number').val(data.hausnummer);
            $('#address_street_number_enhencment').val(data.zusatz_hausnummer);
            $('#address_zip').val(data.plz);
            $('#address_city').val(data.ort);
            $('#address_allow_address').prop('checked', data.adresse_show == "1");
            $('#address_phone').val(data.telefon);
            $('#address_allow_phone').prop('checked', data.telefon_show == "1");
            $('#address_office').val(data.tel_beruf);
            $('#address_email').val(data.email);
            $('#address_allow_email').prop('checked', data.email_show == "1");
            $('#address_rare_email').prop('checked', data.email_selten == "1");
            $('#address_send_email').prop('checked', data.email_erlaubt == "1");
            $('#address_job').val(data.beruf);
        }
        function showChildAddress(data) {
            $('#kind_strasse').val(data.strasse);
            $('#kind_street_number').val(data.hausnummer);
            $('#kind_street_number_enhencement').val(data.zusatz_hausnummer);
            $('#kind_zip').val(data.plz);
            $('#kind_city').val(data.ort);
        }
        function showChildData(child) {
            $('#kind_id').val(child.id);
            $('#kind_vorname').val(child.vorname);
            $('#kind_nachname').val(child.nachname);
            $('#kind_allow_name').prop('checked', child.name_show=="1"),
            $('#kind_birthday_am').val(child.geburtsdatum);
            $('#kind_address_id').val(child.adresse_id);
            $('#kind_address_from option').removeAttr('selected');
            if(child.adresse_id == child.mutter_id) {
                $('#kind_address_from option').get(1).selected = true;
            } else if(child.adresse_id == child.vater_id) {
                $('#kind_address_from option').get(2).selected = true;
            } else {
                $('#kind_address_from option:first').attr('selected', true);
            }
            $('#kind_allow_address').prop('checked', child.adresse_show=="1");
            $('#kind_mother_id').val(child.mutter_id);
            $('#kind_mother_auto_address').val('');
            $('#kind_mother_sorgerecht').prop('checked', child.mutter_sorge=="1");
            $('#kind_father_id').val(child.vater_id);
            $('#kind_father_auto_address').val('');
            $('#kind_father_sorgerecht').prop('checked', child.vater_sorge=="1");
            $('#kind_set_back').val(child.zurueckgestellt);
            $('#kind_allow_image_print').prop('checked', child.bilder_druck=="1");
            $('#kind_allow_image_online').prop('checked', child.bilder_online=="1");
        }
        function resetContract() {
            $('#contract_id').val('');
            $('#bank_id').val('');
            $('bank_name').val('');
            $('#vertrag_ab').val('');
            $('#ausgeschieden_am').val('');
            $('#betreuung_von').val('');
            $('#betreuung_bis').val('');
            $('#darlehen_summe').val('');
            $('#darlehen_eingezogen_am').val('');
            $('#darlehen_zurueck_am').val('');
            $('#aufnahmegebuehr_summe').val('');
            $('#aufnahmegebuehr_eingezogen_am').val('');
            $('#baugeld_summe').val('');
            $('#baugeld_eingezogen_am').val('');
            $('#monatsbeitrag_summe').val('');
            $('#spende_summe').val('');
            $('#hygienebeitrag_summe').val('');
            $('#zuschuss_stadt_summe').val('');
            $('#blk_address').css('display', 'none');
        }
        function resetForm() {
            father = null;
            mother = null;
            $('#txtChild').val('');
            $('#kind_id').val('');
            $('#kind_vorname').val('');
            $('#kind_nachname').val('');
            $('#kind_allow_name').prop('checked', false),
            $('#kind_birthday_am').val('');
            $('#kind_address_id').val('');
            $('#kind_address_from option').removeAttr('selected');
            $('#kind_address_from option:first').attr('selected', true);
            $('#kind_strasse').val('');
            $('#kind_street_number').val('');
            $('#kind_street_number_enhencement').val('');
            $('#kind_zip').val('');
            $('#kind_city').val('');
            $('#kind_allow_address').prop('checked', false);
            $('#kind_mother_id').val('');
            $('#kind_mother_auto_address').val('');
            $('#kind_mother_sorgerecht').prop('checked', false);
            $('#kind_father_id').val('');
            $('#kind_father_auto_address').val('');
            $('#kind_father_sorgerecht').prop('checked', false);
            $('#kind_set_back').val('');
            $('#kind_allow_image_print').prop('checked', false);
            $('#kind_allow_image_online').prop('checked', false);
            $('#contracts_list').empty();
            $('#blk_contract').css('display', 'none');
            $('#blk_address').css('display', 'none');
            $('#address_id').val('');
            $('#address_anrede option').removeAttr('selected');
            $('#address_anrede option:first').attr('selected', true);
            $('#address_german').prop('checked', true);
            $('#address_vorname').val('');
            $('#address_nachname').val('');
            $('#address_allow_name').prop('checked', false);
            $('#address_street').val('');
            $('#address_street_number').val('');
            $('#address_street_number_enhencment').val('');
            $('#address_zip').val('');
            $('#address_city').val('');
            $('#address_allow_address').prop('checked', false);
            $('#address_phone').val('');
            $('#address_allow_phone').prop('checked', false);
            $('#address_office').val('');
            $('#address_email').val('');
            $('#address_allow_email').prop('checked', false);
            $('#address_rare_email').prop('checked', false);
            $('#address_send_email').prop('checked', false);
            $('#address_job').val('');
            resetContract();
        }
    }); 
</script>
